package com.lanou.cart.service.impl;

import com.lanou.cart.mapper.OrderCartMapper;
import com.lanou.cart.service.CartService;
import com.lanou.cart.service.OrderCartService;
import com.lanou.entity.entity.CartItem;
import com.lanou.entity.entity.OrderShippingEntity;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建人   :卢松
 * 创建时间 :2019-07-17
 **/
@Service
public class OrderCartServiceImpl implements OrderCartService {

    @Resource
    private OrderCartMapper orderCartMapper;

    @Resource
    private CartService cartService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<OrderShippingEntity> findByUserId(Long id) {

        List<OrderShippingEntity> shippingEntitiesList = this.orderCartMapper.findByUserId(id);

        return shippingEntitiesList;
    }

    @Override
    public Integer keepItemData(Long[] array, String id) {

        ArrayList<CartItem> cartItemList = new ArrayList<>();

        List<CartItem> list = this.cartService.find(id);

        for (int i = 0; i < array.length; i++) {

            System.out.println(array[i]);

            for (CartItem cartItem : list) {
                if (cartItem.getEntity().getId().equals(array[i])) {
                    cartItemList.add(cartItem);
                }
            }
        }
        ValueOperations ops = this.redisTemplate.opsForValue();

        if (cartItemList.size() > 0){
            ops.set(id+ ":OrderCart", cartItemList);
            return 1;
        }else {
            return 0;
        }

    }

    @Override
    public List<CartItem> getItemData(String id) {

        List<CartItem> cartItemList = this.cartService.find(id+":OrderCart");
        return cartItemList;
    }
}
